package so.shanku.essential.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import so.shanku.essential.activity.ContactUsActivity;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class StringUtil {
	/**
	 *    手机号码的格式验证
	 * @param
	 * @return
	 */
	public static boolean isMobileNO(String mobiles){     
//        Pattern p = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(18[0-9]))\\d{8}$");
//        Matcher m = p.matcher(mobiles);
//        return m.find();
        if(TextUtils.isEmpty(mobiles)){
            return false;
        }else if (!mobiles.startsWith("1")) {
            return false;
        } else if (mobiles.length()!=11) {
             return false;
        } else {
            return true;
        }
    }

	/**
	 * 手机号中间加密
	 * @param str
	 * @return
	 */
	public static String secretMobilePhone(String str){
		StringBuilder stringBuilder=new StringBuilder();
		stringBuilder.append(str.substring(0,3));
		stringBuilder.append("****");
		stringBuilder.append(str.substring(7,11));
		return stringBuilder.toString();
	}
	/**
	 * 邮箱的格式验证
	 * @param
	 * @return
	 */
	public static boolean isEmail(String email) {
		String str = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(email);
		return m.matches();
	}
	/**
	 * 密码的格式验证,只能验证大小字母和数字
	 */
	public static boolean passwordAuthentication(String password){
		Pattern p=Pattern.compile("^[A-Za-z0-9]+$");
		Matcher m = p.matcher(password);
		return m.matches();
	}

	/**
	 * 密码的长度
	 */
	public static boolean passwordAuthenticationLength(String password){
		if(password.length()>5&&password.length()<=16){
			return true;
		} else {
			return false;
		}
	}


	/**
	 *  账号的格式验证
	 * @param
	 * @return不能用中文的汉字和符号!
	 */
	 public static boolean isHaveChineseChar(String str){
   	  if(str.length()==str.getBytes().length){
   		  return true;
   	  }
   	  return false;
   }

	/**
	 * 获取当前时间 yyyy年MM月dd日HH:mm:ss
	 *
	 * @return
	 */
	public static String getData_yyyy_MM_dd(String timeStr) {
		SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
		Date curDate = new Date(timeStr);// 获取当前时间
		String str = formatter.format(curDate);
		return str;
	}

	/**
	 * 获取当前时间 yyyy年MM月dd日HH:mm:ss
	 *
	 * @return
	 */
	public static String getData_yyyy_MM(String timeStr) {
		try {
			SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss " );
			Date curDate = new Date(timeStr);// 获取当前时间
			String str = sdf.format(curDate);
			return str;
		} catch (Exception e) {
			e.printStackTrace();
			return "";
		}
	}

	/**
	 * 格式化价格 商品价格显示 根据需要加
	 * @param price
	 * @return
	 */
	public static String getFormatMoneyWithSign(String price) {
		try {
			if ("".equals(price)) {
				price = "0";
			}
			//Double price_double = Double.parseDouble(price);
			return "￥"+price;
		} catch (Exception e) {
			e.printStackTrace();
			return "￥0.00";
		}
	}

	/**
	 * 格式化价格 带钱币符号   购物车，订单等显示 必须格式化
	 * @param price
	 * @return
	 */
	public static String getFormatMoneyWithSignMust(String price) {
		try {
			if ("".equals(price)) {
				price = "0";
			}

			return "￥"+getFormatMoney(price);
		} catch (Exception e) {
			e.printStackTrace();
			return "￥0.00";
		}
	}

	public static String getFormatMoneyWithSignMust(double price) {
		try {

			return "￥"+getFormatMoney(price);
		} catch (Exception e) {
			e.printStackTrace();
			return "￥0.00";
		}
	}

	/**
	 * 格式化价格 带钱币符号
	 * @param price
	 * @return
	 */
	public static String getFormatMoney(String price) {
		try {
			if ("".equals(price)) {
				price = "0";
			}
			Double price_double = Double.parseDouble(price);
			return getFormatMoney(price_double);
		} catch (Exception e) {
			e.printStackTrace();
			return "0.00";
		}
	}

	/**
	 * 修改 fmt保留两位小数
	 */
	public static String getFormatMoney(double index) {
		if (index == 0) {
			index = 0.00;
		}
		try {
			DecimalFormat fmt = new DecimalFormat("0.00");
			return fmt.format(index);
		} catch (NumberFormatException e) {
			e.printStackTrace();
			return "0.00";
		}
	}

	/**
	 * 获取缩略图路径  SS   96*96
	 */
	public static String getSSImage(String imgUrl) {
		if(TextUtils.isEmpty(imgUrl)){
			return "";
		}
		String subStr=imgUrl.substring(0,imgUrl.length()-11);
		String subfifx=imgUrl.substring(imgUrl.length()-11);
		if(subfifx.startsWith("_source")){

			return subStr+subfifx.replace("_source","_SS");
		}
		return imgUrl;
	}

	/**
	 * 获取缩略图路径  _M   380*380
	 */
	public static String get_MImage(String imgUrl) {
		if(TextUtils.isEmpty(imgUrl)){
			return "";
		}
		String subStr=imgUrl.substring(0,imgUrl.length()-11);
		String subfifx=imgUrl.substring(imgUrl.length()-11);
		if(subfifx.startsWith("_source")){

			return subStr+subfifx.replace("_source","_M");
		}
		return imgUrl;
	}


	/**
	 * 获取缩略图路径  _S
	 */
	public static String get_SImage(String imgUrl) {
		if(TextUtils.isEmpty(imgUrl)){
			return "";
		}
		String subStr=imgUrl.substring(0,imgUrl.length()-11);
		String subfifx=imgUrl.substring(imgUrl.length()-11);
		if(subfifx.startsWith("_source")){

			return subStr+subfifx.replace("_source","_S");
		}
		return imgUrl;
	}
}
